package com.sunday.aomi.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.squareup.picasso.Picasso;
import com.sunday.common.adapter.CommenAdapter;
import com.sunday.common.adapter.ViewHolder;
import com.sunday.common.utils.SpannalbeStringUtils;
import com.sunday.common.widgets.NoScrollListview;
import com.sunday.aomi.R;
import com.sunday.aomi.ui.order.OrderDetailActivity;
import com.sunday.member.constv.ConstValue;
import com.sunday.member.entity.order.OrderItem;
import com.sunday.member.entity.order.OrderProduct;

import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by 刘涛 on 2017/5/27.
 */

public class OrderListAdapter extends RecyclerView.Adapter {

    private Context mContext;
    private List<OrderItem> dataSet;
    private View.OnClickListener onClickListener;

    public OrderListAdapter(Context context, List<OrderItem> datas) {
        this.mContext = context;
        this.dataSet = datas;
    }


    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.list_order_item, null);
        return new OrderHolder(view);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        OrderHolder orderHolder= (OrderHolder) holder;
        OrderItem item=dataSet.get(position);
        int num=0;
        if (item.getProducts()!=null&&item.getProducts().size()>0){
            orderHolder.datas.clear();
            orderHolder.datas.addAll(item.getProducts());
            orderHolder.adapter.notifyDataSetChanged();
            for (OrderProduct product:item.getProducts()){
                num+=product.getNum();
            }
        }

        orderHolder.orderNo.setText(String.format("订单号：%s",item.getOrderNo()));

        orderHolder.totalNum.setText(SpannalbeStringUtils.setTextColor("共计商品：",mContext.getResources().getColor(R.color.black_3)));
        orderHolder.totalNum.append(SpannalbeStringUtils.setTextColor(String.format("%d件",num),mContext.getResources().getColor(R.color.price_yellow)));

        orderHolder.totalMoney.setText(SpannalbeStringUtils.setTextColor("应付款：",mContext.getResources().getColor(R.color.black_3)));
        orderHolder.totalMoney.append(SpannalbeStringUtils.setTextColor(String.format("￥%s",item.getPayAmount().setScale(2,RoundingMode.HALF_UP)),mContext.getResources().getColor(R.color.price_yellow)));
        orderHolder.tvStatus.setText(item.getOrderStatusStr());

        switch (item.getOrderStatus()){
            case ConstValue.STA_TWO:
                orderHolder.btnLayout.setVisibility(View.VISIBLE);
                orderHolder.orderBtn1.setText("取消订单");
                break;
            case ConstValue.STA_THREE:
                orderHolder.btnLayout.setVisibility(View.VISIBLE);
                orderHolder.orderBtn1.setText("确认收货");
                break;
            default:
                orderHolder.btnLayout.setVisibility(View.GONE);
        }

        orderHolder.orderBtn1.setTag(item);
        orderHolder.itemView.setTag(item);
        orderHolder.listView.setTag(item);
        orderHolder.orderBtn1.setOnClickListener(onClickListener);
        orderHolder.itemView.setOnClickListener(onClickListener);

    }

    @Override
    public int getItemCount() {
        return dataSet.size();
    }


    public void setOnClickListener(View.OnClickListener onClickListener) {
        this.onClickListener = onClickListener;
    }

    class OrderHolder extends RecyclerView.ViewHolder {
        @Bind(R.id.order_no)
        TextView orderNo;
        @Bind(R.id.tvStatus)
        TextView tvStatus;
        @Bind(R.id.no_scroll_list_view)
        NoScrollListview listView;
        @Bind(R.id.total_num)
        TextView totalNum;
        @Bind(R.id.total_money)
        TextView totalMoney;
        @Bind(R.id.rl_total_layout)
        RelativeLayout rlTotalLayout;
        @Bind(R.id.order_btn2)
        Button orderBtn2;
        @Bind(R.id.order_btn1)
        Button orderBtn1;
        @Bind(R.id.btn_layout)
        RelativeLayout btnLayout;
        @Bind(R.id.item_view)
        LinearLayout itemView;

        private CommenAdapter<OrderProduct> adapter;
        private List<OrderProduct> datas=new ArrayList<>();

        public OrderHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this,itemView);
            adapter = new CommenAdapter<OrderProduct>(mContext,datas, R.layout.list_order_confirm_product) {
                @Override
                public void convert(ViewHolder helper, OrderProduct item, int position) {
                    bind(helper, item);
                }
            };
            listView.setAdapter(adapter);
            adapter.notifyDataSetChanged();
            listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    OrderItem orderItem= (OrderItem) listView.getTag();
                    Intent intent=new Intent(mContext, OrderDetailActivity.class);
                    intent.putExtra("orderId",orderItem.getId());
                    mContext.startActivity(intent);
                }
            });
        }
    }

    private void bind(ViewHolder helper, OrderProduct item) {
        ImageView imageView = helper.getView(R.id.product_img);
        if (!TextUtils.isEmpty(item.getImage())) {
            Picasso.with(mContext)
                    .load(item.getImage())
                    .error(R.mipmap.ic_default)
                    .resize(150, 150)
                    .into(imageView);
        }
        ((TextView) helper.getView(R.id.product_title)).setText(item.getName());
        ((TextView) helper.getView(R.id.product_price)).setText(String.format("￥%s", item.getPrice().setScale(2, RoundingMode.HALF_UP)));
        ((TextView) helper.getView(R.id.product_num)).setText(String.format("X%d", item.getNum()));
        ((TextView) helper.getView(R.id.product_spec)).setText(item.getStandard());
    }


}
